package com.itstyle.quartz.config;

import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.itstyle.quartz.service.dao.IJobService;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;

/**
 * 创建时间	2020年4月3日
 */
@Component
public class TaskRunner implements ApplicationRunner{
    
	private final static Logger LOGGER = LoggerFactory.getLogger(TaskRunner.class);
	
	@Autowired
    private IJobService jobService;
	@Autowired
    private Scheduler scheduler;
	
	@Override
    @Transactional
    public void run(ApplicationArguments var) throws Exception{
        /**
         * 系统启动的时候会初始化一个任务
         *
         */
        /*Long count = jobService.listQuartzEntity(null);
        if(count == 0){
            LOGGER.info("初始化测试任务");
            QuartzEntity quartz = new QuartzEntity();
            quartz.setJOBNAME("test01");
            quartz.setJOBGROUP("test");
            quartz.setDESCRIPTION("测试任务");
            quartz.setJOBCLASSNAME("com.itstyle.quartz.job.ChickenJob");
            quartz.setCRONEXPRESSION("");
            quartz.setJOBMETHODNAME("test1");
            Class cls = Class.forName(quartz.getJOBCLASSNAME()) ;
            cls.newInstance();
            //构建job信息
            JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJOBNAME(),
                    quartz.getJOBGROUP())
                    .withDescription(quartz.getDESCRIPTION()).build();
            job.getJobDataMap().put("jobMethodName", "test1");
            // 触发时间点
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCRONEXPRESSION());
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger"+quartz.getJOBNAME(), quartz.getJOBGROUP())
                    .startNow().withSchedule(cronScheduleBuilder).build();
            //交由Scheduler安排触发
            scheduler.scheduleJob(job, trigger);
        }*/
        /*this.txt2String(jobService);*/
    }

        public  void txt2String(IJobService jobService) {
            File  file = new File("D:\\p1000\\HISDB_HISDB_MANALOG_DATA_SCADA_20200722.sql");
            int num =0 ;
             try {
                 BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GB18030"));
                 String s = null;
                 while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                     if(s != null && !"".equals(s)){
                         num=num+1;
                         System.out.println("已写入数据："+num+"条");
                         jobService.saveYcData(s);
                     }
                }
                 br.close();

            } catch (Exception e) {
                 e.printStackTrace();

            }
            System.out.println("你可以在项目启动的时候，调用某个特定的任务！");

        }


    }